package com.boot.security.server.controller.system;

import java.util.List;

import com.boot.security.server.dao.mffl.MfflInformationDao;
import com.boot.security.server.model.mffl.MfflInformation;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.mffl.MfflTypeDao;
import com.boot.security.server.model.mffl.MfflType;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@RestController
@RequestMapping("/mfflTypes")
public class MfflTypeController {

    @Autowired
    private MfflTypeDao mfflTypeDao;

    @Autowired
    private MfflInformationDao mfflInformationDao;



    @ApiOperation("广告用社区列表")
    @PreAuthorize("hasAnyAuthority('sys:mfflTypes:query')")
    @GetMapping(value = "/adminGetInformationType")
    public List<MfflType> getScCommunityList(){
        return mfflTypeDao.getInformationType();
    }


    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:mfflTypes:add')")
    @ApiOperation(value = "保存")
    public MfflType save(@RequestBody MfflType mfflType) {
        mfflType.setType(mfflTypeDao.getInformationIdByName("资讯"));
        mfflTypeDao.save(mfflType);
        return mfflType;
    }


    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:mfflTypes:query')")
    @ApiOperation(value = "根据id获取")
    public MfflType get(@PathVariable Long id) {
        return mfflTypeDao.getById(id);
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys:mfflTypes:add')")
    @ApiOperation(value = "修改")
    public MfflType update(@RequestBody MfflType mfflType) {

        mfflTypeDao.update(mfflType);

        return mfflType;
    }

    @GetMapping
    @PreAuthorize("hasAnyAuthority('sys:mfflTypes:query')")
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflTypeDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflType> list(PageTableRequest request) {
                return mfflTypeDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:mfflTypes:del')")
    @ApiOperation(value = "删除")
    public ReturnBean delete(@PathVariable Long id) {

        int count = this.mfflInformationDao.getMyInformationCountByType(id);
        if (count != 0 ){
            return new ReturnBean("201","该分类下有未删除的资讯不可删除！");
        }
        mfflTypeDao.delete(id);
        return new ReturnBean();
    }
}
